using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
namespace DataPaper
{
    static class Pages
    {
        static Dictionary<int, Byte[]> dico = new Dictionary<int, byte[]>();

        public static string Msg()
        {
            if (dico.Count == 0)
                return "#page: 0";
            List<int> lst = new List<int>(dico.Keys);
            lst.Sort();
            if (lst[lst.Count - 1] == lst.Count - 1)
                return "#page: "+lst.Count;
            int k = 0;
            do
            {
                if (lst[k] != k)
                {
                    return "missing:" + (k+1) + "\t#page: "+(lst[lst.Count-1]+1);
                }
                ++k;
            } while (true);


        }

        public static void Add(int page, List<Byte> info)
        {
            dico[page]=info.ToArray();
        }
        public static void Write(string fileName)
        {
            using (BinaryWriter bw = new System.IO.BinaryWriter(
                File.Open(fileName, FileMode.Create)))
            {
                for (int i = 0; i < dico.Count; ++i)
                    bw.Write(dico[i]);
           }
        }


    }

    class Zoom:IDisposable
    {
        static private Zoom current = new Zoom("");
        //FIXME: to be cleaned.
        public double init_u = 7.1;
        public Point init_top_left = new Point(300, 58);
        public Bitmap bmp;

        //private int zoomFactor=4;

        //public Info info;
        public void Run()
        {

        }

        public static Zoom Cur
        {
            get
            {
                return current;
            }
            set
            {
                if (current != null)
                    current.Dispose();
                current = value;
            }
        }


        public Point MouseToPoint(int m_r, int m_c,
            System.Windows.Forms.PictureBox box,
            double z)
        {
            return new Point(
                this.init_top_left.r - box.Height / 2 / z + m_r / z,
                this.init_top_left.c - box.Width / 2 / z + m_c / z);                
        }

        private void Plot(Point p, double u, bool black)
        {
            int red = black ? 255 : 0;
            int green = black ? 0 : 255;
            for (int i = (int)(-u/4); i <(int)(u/4)+1; ++i)
                if (i != 0)
                {
                    //H
                    {
                        int rr = (int)(p.r);
                        int cc = p.c + i;
                        Color col = bmp.GetPixel(cc, rr);
                        this.bmp.SetPixel(cc, rr, Color.FromArgb(red, green, col.B));
                    }
                    //Left
                    {
                        int rr = p.r + i;
                        int cc = (int)(p.c);
                        Color col = bmp.GetPixel(cc, rr);
                        this.bmp.SetPixel(cc, rr, Color.FromArgb(red, green, col.B));
                    }
                }

        }
        public void AddSmallErrors(List<SmallError> err)
        {
            foreach (SmallError e in err)
            {
                Plot(e.p, e.u, e.correctedToB);
            }
        }

        public void AddLargeErrors(List<LargeError> err)
        {
            foreach (LargeError e in err)
            {
                for (int ri = 0; ri < 17; ++ri)
                    for (int ci = 0; ci < 17; ++ci)
                        Plot(
                            new Point(e.p.r+e.u_r*ri, e.p.c+e.u_c*ci),
                            0.5*(e.u_r+e.u_c), e.block[ri,ci]);
            }
        }

        public void Print(System.Windows.Forms.PictureBox box,
                          int center_r, int center_c, double z)
        {
            if (bmp == null)
                return;
            if (box.Image != null)
            {
                box.Image.Dispose();
                box.Image = null;
            }

            Bitmap tmp = new Bitmap(box.Width,
               box.Height);

            int xr = (int)(center_r - box.Height / 2 / z);
            int xc = (int)(center_c - box.Width / 2 / z);
            for (int r = 0; r < box.Height; r++)
                for (int c = 0; c < box.Width; c++)
                {
                    int or = (int)(xr + r / z);
                    int oc = (int)(xc + c / z);

                    if (or >= 0 && oc >= 0 &&
                        oc < bmp.Width &&
                        or < bmp.Height)
                        tmp.SetPixel(c, r,
                            bmp.GetPixel(oc, or));
                    else
                        tmp.SetPixel(c, r,
                            Color.Red);
                }
            box.Image = tmp;
        }

        public Zoom(string filePath)
        {
            if (filePath == "")
                bmp = null;
            else
            {
                using (Bitmap jpg = new Bitmap(filePath))
                {
                    bmp = new Bitmap(jpg.Width, jpg.Height);
                    Graphics grPhoto = Graphics.FromImage(bmp);
                    grPhoto.DrawImage(jpg, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width,
                    bmp.Height, GraphicsUnit.Pixel);
                }
            }
        }
        public void Dispose()
        {
            if(bmp != null)
                bmp.Dispose();
        }
    }
}
